# -*- coding:utf-8 -*-
from config import CONFIG
import sys, argparse, os, datetime, pymongo, time
from modules import DouyinJson, DATABASE, DouYinDownload, YoutubeUpload, DouyinTransform
from config.config import Confg
from time import sleep
import json

class Main(object):
    def __init__(self):
        """
        初始化
        """
        # 分组名称
        self.group_name = ""
        self.file_list = []
        self.download = False
        self.config = False
        self.num = 0
        self.upload = False


    def hello(self):
        """
        文本提示
        :return:
        """
        print("""
            ==========================================
            谢谢使用DQ命令行，用来下载抖音视频，并且上传到youtube
            ==========================================
            
            """)

    def check_arg(self, args):
        """
        检测参数
        :param args:
        :return:
        """
        if args.name != None:
            self.group_name = args.name
        if args.num != None:
            self.num = args.num

        # 处理json文件
        if args.json != None:
            # 检测json文件夹是否存在
            if os.path.isdir(args.json) == False:
                print("json参数不是文件夹")
                os._exit(1)

            if (args.name == None):
                print("json参数必须同时输入name参数")
                os._exit(1)

            for tmp in os.listdir(args.json):
                self.file_list.append(os.path.join(args.json, tmp))

        # 是否下载
        if args.download != None:
            if (args.name == None):
                print("download参数必须同时输入name参数")
                os._exit(1)
            if args.num <= 0:
                print("num参数不能小于0")
            if args.download == 1:
                self.download = args.download

        if args.config != None:
            if (args.name == None):
                print("config 参数必须同时输入name参数")
                os._exit(1)
            self.config = args.config


        # 是否上传
        if args.upload != None:
            if args.upload == 1:
                self.upload = args.upload

    def downloadVideo(self):
        """
        下载视频
        """
    def video_config_name(self, dir=''):
        '''
        生成随机的视频json文件
        :param dir:文件夹
        :return:
        '''
        while True:
            now = datetime.datetime.now()
            file_name = now.strftime('%Y-%m-%d_%H-%M-%S')
            full_path = dir + '/' + file_name
            if os.path.isfile(full_path) == False:
                file = open(full_path, 'w')
                file.close()
                break
            else:
                time.sleep(1)
        return full_path


if __name__ == '__main__':
    main = Main()
    main.hello()
    config_tool = Confg()
    # 视频数据json文件存储
    video_json_dir = config_tool.get_video_config_dir()
    # 下载文件
    video_download_dir = CONFIG.get_video_dir()
    parser = argparse.ArgumentParser()
    parser.add_argument("--name", help="分组名称 处理分组", type=str)
    parser.add_argument("--json", help="json文件所在的文件夹", type=str)
    parser.add_argument("--download", help="是否下载(1:上传、0:不下载)", type=int)
    parser.add_argument("--num", help="上传或者下载数量", type=int)
    parser.add_argument("--config", help="是否生成配置文件(1:生成、0:不生成)", type=int)
    parser.add_argument("--upload", help="是否上传(1:上传、0:不上传)", type=int)
    args = parser.parse_args()
    main.check_arg(args)

    # 从json文件中获取数据
    data_save = []
    if main.group_name != None and main.file_list != []:
        print("log: json文件夹下共： %d 个文件" % (len(main.file_list)))
        print("log: json文件夹下共： ", main.file_list)
        douyinJson = DouyinJson()
        data_save = douyinJson.format_json(main.file_list)


    # json 获取的数据写入mongodb中
    if main.group_name != None and data_save != []:
        print('log: 开始插入数据')
        update_count = insert_count = 0
        for tmp in data_save:
            where = {'video_id': tmp['video_id']}
            exist_log = DATABASE['video_log'].find_one(where)
            if exist_log == None:
                # 数据不存在，插入
                tmp['group_name'] = main.group_name
                tmp['create_time'] = datetime.datetime.now()
                DATABASE['video_log'].insert_one(tmp)
                insert_count += 1
            else:
                # 数据存在更新
                tmp_save_data = {
                    'group_name': main.group_name,
                    'upload_count': tmp['upload_count'],
                    'comment_count': tmp['comment_count'],
                    'digg_count': tmp['digg_count'],
                    'share_count': tmp['share_count']
                }
                DATABASE['video_log'].update_one(where, {"$set": tmp_save_data})
                update_count += 1

        print('log: 插入数据完成。 insert: %d, update: %d ' % (insert_count, update_count))


    if main.group_name != None and main.download == 1 and main.num > 0:
        douyin_download_tool = DouYinDownload()

        print('\r\nlog: 数据提取pickup开始\r\n')
        update_data = DATABASE['video_log'].find({'group_name': main.group_name, 'pick_up': 0}).sort([("upload_count", pymongo.DESCENDING)]).limit(main.num)
        for tmp in update_data:
            DATABASE['video_log'].update({'video_id': tmp['video_id']}, {'$set': {'pick_up': 1}})

        print('\r\nlog: 开始下载视频文件文件\r\n')
        douyin_transform_tool = DouyinTransform()
        upload_data = DATABASE['video_log'].find({'group_name': main.group_name, 'pick_up': 1, 'is_download': 0}).sort([("upload_count", pymongo.DESCENDING)]).limit(main.num)
        douyin_download_count = 0
        for tmp in upload_data:
            tmp_save_dir = video_download_dir + "/" + tmp['video_id']
            real_download_url = douyin_transform_tool.get_real_url(tmp['download_url'])
            if real_download_url == '':
                continue

            douyin_download_return = douyin_download_tool.video_download(real_download_url, tmp_save_dir)
            if douyin_download_return == 1:
                DATABASE['video_log'].update({'video_id': tmp['video_id']}, {'$set': {'is_download': 1}})
                douyin_download_count += 1
            sleep(5)

        print('\r\nlog: 视频下载完成, 应下载个数：%d, 下载成功个数：%d\r\n' % (main.num, douyin_download_count))


    # 生成待上传json文件
    if main.config == 1 and main.num > 0:
        print('\r\nlog: 生成json文件开始\r\n')
        youtube_data = DATABASE['video_log'].find({'group_name': main.group_name, 'pick_up': 1, 'is_download': 1, 'is_upload': 0}).sort([("upload_count", pymongo.DESCENDING)]).limit(main.num)
        file_content = []
        upload_data = []
        for tmp in youtube_data:
            file_content.append({
                'video_id': tmp['video_id'],
                'title': tmp['title'],
                'desc': tmp['desc'],
            })
            upload_data.append(tmp['video_id'])

        if len(upload_data) > 0:
            json_file_name = main.video_config_name(video_json_dir)
            # 写入文件
            with open(json_file_name, 'w') as tmp_file:
                json.dump(file_content, tmp_file)

            # 最后修改数据库为已上传状态
            for tmp in upload_data:
                DATABASE['video_log'].update({'video_id': tmp}, {'$set': {'is_upload': 1}})

        print('\r\nlog: 生成json文件结束，生成数据：%d\r\n' % len(upload_data))





    if main.upload == 1:
        print('\r\nlog: 开始上传视频文件\r\n')
        file_dir = []
        for tmp in os.listdir(video_json_dir):
            file_dir.append(os.path.join(video_json_dir, tmp))

        if len(file_dir) == 0:
            print('\r\nlog: 结束上传视频文件上传数量为0\r\n')
            exit(1)

        upload_data = []
        for tmp in file_dir:
            with open(tmp, 'r') as tmp_file:
                transform = json.load(tmp_file)
                for tmp_data in transform:
                    upload_data.append(tmp_data)

        if len(upload_data) == 0:
            print('\r\nlog: 结束上传视频文件上传数量为0\r\n')
            exit(1)


        youtube_tool = YoutubeUpload()
        json_config_dir = CONFIG.get_config_json_dir()


        failed_data = []
        youtube_upload_count = 0
        for tmp in upload_data:
            tmp_path = video_download_dir + '/' + tmp['video_id']
            if os.path.isfile(tmp_path):
                youtube_upload_return = youtube_tool.upload(tmp_path, tmp['title'], tmp['desc'], json_config_dir)
                if youtube_upload_return == 1:
                    youtube_upload_count += 1
                else:
                    failed_data.append({
                        'video_id': tmp['video_id'],
                        'title': tmp['title'],
                        'desc': tmp['desc']
                    })
            else:
                failed_data.append(tmp)

        print('log: 视频上传完成, 应上传个数：%d, 上传成功个数：%d' % (len(upload_data), youtube_upload_count))
